package client;

import java.io.Serializable;

import javax.swing.table.AbstractTableModel;

/**
 * 
 * Approves users who applied for membership
 * Disapproves members
 *
 */
public class MemberApproval extends AbstractTableModel implements Serializable {

	/**
	 * Table columns names
	 */
	private String[] columnNames={"User ID","User Name","Approved"};
	/**
	 * tables data
	 */
	private Object[][] data;
	/**
	 * variable to rule if data update is needed
	 */
	private int update;
	/**
	 * member id number
	 */
	private int uid;
	/**
	 * variable to rule if user is approved/disapproved
	 */
	private int approve;
	
	/**
	 * starts with downloading members data
	 */
	public MemberApproval()
	{
		downloadData();
	}
	
	public int getColumnCount() 
	{
		return columnNames.length;
	}
	
	public boolean isCellEditable(int row, int col) {
        if (col == 2) {
            return true;
        } else {
            return false;
        }
	}
	
	public void setValueAt(Object value, int row, int col) {
        data[row][col] = value;
        fireTableCellUpdated(row, col);
    }
	
	public String getColumnName(int column) {
		return columnNames[column];
		}
	
	public Object getValueAt(int rowIndex, int columnIndex) {
		return data[rowIndex][columnIndex];
		}
	
	public int getRowCount() 
	{
		return data.length;
	}
	
	@Override
	public Class<?> getColumnClass(int columnIndex) {
		return data[0][columnIndex].getClass();
		}
	
	public String[] getColName()
	{
		return columnNames;
	}
	
	/**
	 * Gets member ID number
	 * @return member ID number
	 */
	public int getUID()
	{
		return uid;
	}
	
	/**
	 * Gets approvals status
	 * @return 1 approved,else 0
	 */
	public int getApprove()
	{
		return approve;
	}
	
	/**
	 * Gets update request
	 * @return 1 for update,0 download data
	 */
	public int getUpdate()
	{
		return update;
	}
	
	/**
	 * Gets members table data
	 * @return table data
	 */
	public Object[][] getTableData()
	{
		return data;
	}
	
	/**
	 * gets data from DB
	 */
	void downloadData()
	{
		update=0;
		ObjHandle.sendToServer(this);
		data= (Object[][])Main.clie.getResult();
	}
	
	/**
	 * Updates members data (approved/disapproved)
	 * @param row selected user (by row in table)
	 * @param app 1 approved,0 disapproved
	 * @return 1 update succeeded,else 0
	 */
	public int updateData(int row,int app)
	{
		update=1;
		String s=String.valueOf(data[row][0]);
		uid=Integer.parseInt(s);
		approve=app;
		
		ObjHandle.sendToServer(this);
		
		return (Integer)Main.clie.getResult();
	}
}